﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;

/// <summary>
///Upload 的摘要说明
/// </summary>
public class Upload
{
    public Upload()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public static string SavePicture(string  strOldPath)
    {
        HttpRequest Request = HttpContext.Current.Request;
        HttpServerUtility Server = HttpContext.Current.Server;
        string strResult = strOldPath;
        if (Request.Files.Count > 0)
        {
            string filter = ".jpg|.gif|.png";
            HttpPostedFile upfile = Request.Files[0];
            string strExt = System.IO.Path.GetExtension(upfile.FileName);
            if (filter.IndexOf(strExt) < 0)
            {
                throw new Exception(string.Format("上传图片格式非法（{0}）", filter));
            }
            string strFileName = DateTime.Now.ToString("yyyyMMddhh24mmss")+"_big"  + strExt;
            strResult = "/upload/" + strFileName;
            upfile.SaveAs(Server.MapPath(strResult));
            string strThumbnail = strResult.Replace("big", "small");
            GenerateThumbnail.GenThumbnail(Server.MapPath(strResult), Server.MapPath(strThumbnail), 300, 300);
            try
            {
                System.IO.File.Delete(Server.MapPath(strOldPath));
                System.IO.File.Delete(Server.MapPath(strOldPath.Replace("Big", "Small")));
            }
            catch { }
        }
        return strResult;
    }


}